var table;
var tableUser;
//相关业务逻辑
$(function(){
	page.bindTag();
	page.getUser();
	nameToIdUtil.getProductionDepartment("productionDepartment");
});


var page={//页面总变量
	    workShopId:'',
		bindTag:function(){
			$("#productionDepartment").bind("change",function(){
				var deptId=$("#productionDepartment").val();
				nameToIdUtil.getWorkShopNameAndId("workShop",deptId);
			});
			
			//点击查询
			$("#queryOrderBtn").bind("click",function(){
				var deptId=$("#productionDepartment option:selected").val();
				var workShop=$("#workShop option:selected").val();
				var selectWhere=$("#selectWhere").val(); 
				page.getWorkShop(deptId,workShop,selectWhere);
			});
			
			//设置外协审批人
			$("#setPutOutReportBtn").bind("click",function(){
				var workShopId=-1;
				page.workShopId=workShopId;
				ajaxUtil.ajaxRuquest("basicData/getWorkShopApprovalReportUser",{'workShopId':workShopId,"type":2},function(data){
					if(data!=null){
						$("#thisReport").text(data.userName);
						$("input[name='checkUser']:radio[value='"+data.userId+"']").attr('checked','true');
					}else{
						$("#thisReport").text("无");
						$("input[name='checkUser']").prop('checked',false);
					}
					$("#userModal").modal("show");
				});
			});
			
			
			//选择工序
			$('#workShopTable tbody').on('click', '.setWorkShopPeople', function () {
				var workShopId=$(this).attr("id");
				page.workShopId=workShopId;
				ajaxUtil.ajaxRuquest("basicData/getWorkShopApprovalReportUser",{'workShopId':workShopId,"type":1},function(data){
					if(data!=null){
						$("#thisReport").text(data.userName);
						$("input[name='checkUser']:radio[value='"+data.userId+"']").attr('checked','true');
					}else{
						$("#thisReport").text("无");
						$("input[name='checkUser']").prop('checked',false);
					}
					$("#userModal").modal("show");
				});
			});
			
			//点击保存按钮
			$("#setWorkShopModalBtn").bind("click",function(){
				var userId=$("input[name='checkUser']:checked").val();
				var type=1;
				if(userId==''){
					layAlert("请选择要设置的用户");
					return false;
				}
				if(page.workShopId==-1){
					type=2;
				}
				ajaxUtil.ajaxRuquest("basicData/setWorkShopApprovalReportUser",{'workShopId':page.workShopId,"userId":userId,"type":type},function(data){
					layer.msg(data,{icon:1});
					table.ajax.reload( null, false);
					$("#userModal").modal("hide");
				});
			});
		},
		getWorkShop:function(deptId,workShopId,selectWhere){
			$("#workShopTable").show();
			table=ajaxUtil.dataTablesAjax("workShopTable",10,"basicData/getWorkShopPage",
					{'deptId':deptId,
				     'workShopId':workShopId,
				     'selectWhere':selectWhere
				     },[
					{"data":"productionLine"},
					{"data":"workshopName"},
					{"data":"userName"},
					{"data":"id",render: function (data,type,row) {
						return "<span  class='setWorkShopPeople btn btn-link' id='"+data+"'>设置</span>";
					}}
			]);
		},
		getUser:function(){
			tableUser=ajaxUtil.dataTablesAjaxNotFxiaction("userTable",10,"basicData/getUser",
					{},[
						{"data":"userName"},
						{"data":"deptName"},
						{"data":"workShopName"},
						{"data":"phone"},
						{"data":"id",render: function (data,type,row) {
							return "<input type='radio'  name='checkUser' value='"+data+"' >";
						}}
						]);
		}
		
		
}
